package org.lex.awt;

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class GraphicsTest extends JFrame {
	private JPanel pane = null;

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				GraphicsTest thisClass = new GraphicsTest();
				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				thisClass.setVisible(true);
			}
		});
	}

	public GraphicsTest() {
		super();
		initialize();
	}

	private void initialize() {
		this.setSize(300, 200);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setContentPane(getPane());
		this.setTitle("JFrame");
	}

	private JPanel getPane() {
		if (pane == null) {
			pane = new JPanel() {
				protected void paintComponent(Graphics g) {
					Graphics2D g2 = (Graphics2D) g;
					g2.setColor(Color.blue);
					FontMetrics fm = g2.getFontMetrics();
					String text = "Application Title";
					Rectangle2D b = fm.getStringBounds(text, g2);
					b.setFrame(50, 50, b.getWidth(), b.getHeight());
					g2.draw(b);
					int height = (int) Math.round(b.getHeight());
					int y = 50 + GraphicsUtils.getVerticalPlacement(fm, height);
					g2.drawString(text, 50, y);
				}
			};
		}
		return pane;
	}
}
